//
//  UUIDHelper.m
//  Protocol
//
//  Created by  user on 11-1-10.
//  Copyright 2011 GuangZhouXuanWu. All rights reserved.
//

#import "XMSGUID.h"

@implementation XMSGUID
{
    GUID _guid;
}

@synthesize guid = _guid;

- (id)initWithGUID:(GUID)guid
{
    self = [super init];
    _guid = guid;
    return self;
}

- (id)init
{
    self = [super init];
    memset(&_guid, 0, sizeof(GUID));
    return self;
}

+ (XMSGUID*)newGuid
{
    return [[[XMSGUID alloc]initWithGUID:[XMSGUID GUIDBytes]]autorelease_mm];
}

- (NSString*)toLowString
{
    return [[XMSGUID GUIDStringWithGUIDBytes:_guid] lowercaseString];
}

- (NSString*)toString
{
    return [XMSGUID GUIDStringWithGUIDBytes:_guid];
}

- (BOOL)isEqualToCGUID:(XMSGUID *)guid
{
    return ![XMSGUID compareGUID:_guid other:guid.guid];
}

+ (void)resetGUID:(GUID *)guid
{
    memset(guid, 0, sizeof(GUID));
}

+ (GUID)emptyGUID
{
    GUID g;
    memset(&g, 0, sizeof(GUID));
    return g;
}

+ (NSString *)GUIDString
{
    CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault);
    NSString *s = [(__bridge_mm NSString *) CFUUIDCreateString(kCFAllocatorDefault, uuid) autorelease_mm];
    CFRelease(uuid);
    return s;
}

+ (NSString *)GUIDLowString
{
    return [[XMSGUID GUIDString] lowercaseString];
}

+ (GUID)GUIDBytes
{
    CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault);
    CFUUIDBytes u = CFUUIDGetUUIDBytes(uuid);
    CFRelease(uuid);
    GUID g = {0};
    memcpy(&g,&u,sizeof(GUID));
    return g;
}

+ (NSString *)GUIDStringWithGUIDBytes:(GUID)guid
{
    CFUUIDBytes t = {0};
    memcpy(&t, &guid, sizeof(GUID));
    t.byte0 = guid.byte3;
    t.byte1 = guid.byte2;
    t.byte2 = guid.byte1;
    t.byte3 = guid.byte0;
    t.byte4 = guid.byte5;
    t.byte5 = guid.byte4;
    t.byte6 = guid.byte7;
    t.byte7 = guid.byte6;
    CFUUIDRef uuid = CFUUIDCreateFromUUIDBytes(kCFAllocatorDefault, t);
    NSString *s = [(__bridge_mm NSString *) CFUUIDCreateString(kCFAllocatorDefault, uuid) autorelease_mm];
    CFRelease(uuid);
    return s;
}

+ (NSString *)GUIDLowStringWithGUIDBytes:(GUID)guid
{
    return [[XMSGUID GUIDStringWithGUIDBytes:guid] lowercaseString];
}

+ (GUID)GUIDBytesWithGUIDString:(NSString *)str
{
    GUID guid = {0};
    if (str.length > 0)
    {
        CFUUIDRef uuid = CFUUIDCreateFromString(kCFAllocatorDefault, (CFStringRef) str);
        CFUUIDBytes t = CFUUIDGetUUIDBytes(uuid);
        CFRelease(uuid);
        memcpy(&guid,&t, sizeof(GUID));
        guid.byte0 = t.byte3;
        guid.byte1 = t.byte2;
        guid.byte2 = t.byte1;
        guid.byte3 = t.byte0;
        guid.byte4 = t.byte5;
        guid.byte5 = t.byte4;
        guid.byte6 = t.byte7;
        guid.byte7 = t.byte6;
    }
    return guid;
}

- (BOOL)isEqual:(id)object
{
    return [XMSGUID compareGUID:_guid other:((XMSGUID*)object).guid];
}

+ (NSInteger)compareGUID:(GUID)p other:(GUID)q
{
    return memcmp(&p, &q, sizeof(GUID));
}

+ (BOOL)isEmptyGUID:(GUID)guid
{
    return [XMSGUID compareGUID:guid other:[XMSGUID emptyGUID]]==0;
}

+ (BOOL)isEmptyGUIDString:(NSString*)szGuid
{
    if(szGuid.length<=0)
        return YES;
    return [szGuid isEqualToString:@"00000000-0000-0000-0000-000000000000"];
}

- (NSString *)description
{
    return [NSString stringWithFormat:@"%@,%@", [super description], [XMSGUID GUIDStringWithGUIDBytes:_guid]];
}

@end
